import 'package:flutter/material.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/seckill_order_detail_info_item.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_order_summary_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/vo/order_aggregates_vo.dart';

class SeckillOrderSummaryPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '汇总信息',
        backgroundColor: bgWhiteColor,
      ),
      body: GetBuilder<SeckillOrderSummaryPageController>(
        init: SeckillOrderSummaryPageController(),
        builder: (controller) {
          if (controller.orderAggregatesVo.value.data!.length <= 0) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    'assets/icons/icon_order_list_def.png',
                    width: 273.w,
                    height: 146.h,
                  ),
                  SizedBox(height: 10.h),
                  Text(
                    '还没订单~去下一单！',
                    style: TextStyle(
                      color: textSubtitleColor,
                      fontSize: 14.sp
                    ),
                  )
                ],
              ),
            ); 
          }
          return RefreshUtil.easyRefresh(
            onRefresh: () async {
              controller.getOrderAggregates();
            },
            onLoad: () async {
              controller.onloadOrderAggregates();
            },
            slivers: [
              SliverToBoxAdapter(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 22.w),
                  margin: EdgeInsets.symmetric(vertical: 12.h),
                  child: ButtonUtil(
                    text: '下载汇总报表', 
                    fontSize: 14.sp, 
                    borderRadius: 5.w, 
                    height: 45.h, 
                    bgColors: [
                      orderSummaryBtnBg,
                      orderSummaryBtnBg
                    ],
                    onTop: () {
                      controller.downloadFile();
                    }
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
                  OrderAggregatesItem item = controller.orderAggregatesVo.value.data![index];
                  return Container(
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    margin: EdgeInsets.only(bottom: 12.h),
                    child: Container(
                      decoration: BoxDecoration(
                        color: bgWhiteColor,
                        borderRadius: BorderRadius.circular(5.w)
                      ),
                      child: Column(
                        children: [
                          Container(
                            padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 12.h),
                            child: Column(
                              children: [
                                SeckillOrderDetailInfoItem(title: '收款人姓名', value: '${item.accountName}', showCopy: true),
                                SeckillOrderDetailInfoItem(title: '银行卡账户', value: '${item.cardNumber}', showCopy: true),
                                SeckillOrderDetailInfoItem(title: '开户行名称', value: '${item.bank}', showCopy: true),
                                SeckillOrderDetailInfoItem(title: '注册手机号', value: '${item.mobile}', showCopy: true),
                                SeckillOrderDetailInfoItem(title: '合计待付款', value: '${item.totalAmount}', showCopy: true),
                              ],
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 8.w),
                            child: Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.only(left: 4.w, right: 4.w, top: 12.h, bottom: 12.h),
                                  alignment: Alignment.centerLeft,
                                  decoration: BoxDecoration(
                                    border: Border(
                                      top: BorderSide(
                                        width: .5,
                                        color: lineColor
                                      )
                                    )
                                  ),
                                  child: Text(
                                    '关联订单列表（${item.number}）',
                                    style: TextStyle(
                                      color: textBlackColor,
                                      fontSize: 14.sp,
                                      fontWeight: FontWeight.bold
                                    ),
                                  )
                                ),
                                AnimatedContainer(
                                  duration: Duration(milliseconds: 500),
                                  padding: EdgeInsets.symmetric(horizontal: 4.w),
                                  height: item.openStatus == 0 ? 0 : item.orders!.length * 34.h,
                                  child: Column(
                                    children: item.orders!.map((orderItem) {
                                      return Flexible(
                                        child: Container(
                                          height: 34.h,
                                          child: Row(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Expanded(child: Text(
                                                '${orderItem.orderSn}',
                                                overflow: TextOverflow.ellipsis,
                                                maxLines: 1,
                                                style: TextStyle(
                                                  color: textSubtitleColor,
                                                  fontSize: 14.sp
                                                ),
                                              )),
                                              Text(
                                                '¥${orderItem.totalAmount}',
                                                style: TextStyle(
                                                  color: textBlackColor,
                                                  fontSize: 14.sp
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                      );
                                    }).toList(),
                                  ),
                                )
                              ],
                            )
                          ),
                          InkWellUtil.shakeProofInkWell(Container(
                            width: 343.w,
                            height: 41.h,
                            decoration: BoxDecoration(
                              border: Border(
                                top: BorderSide(
                                  width: .5,
                                  color: lineColor
                                )
                              )
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  item.openStatus == 0 ? '展开' : '收起',
                                  style: TextStyle(
                                    color: mainTextColor,
                                    fontSize: 12.sp
                                  ),
                                ),
                                SizedBox(width: 3.w),
                                Image.asset(
                                  item.openStatus == 0 ? 'assets/icons/icon_order_open.png' : 'assets/icons/icon_order_put_away.png',
                                  width: 10.w,
                                  height: 10.w
                                )
                              ],
                            ),
                          ), () {
                            item.openStatus = item.openStatus == 0 ? 1 : 0;
                            controller.update();
                          })
                        ],
                      ),
                    ),
                  );
                },
                childCount: controller.orderAggregatesVo.value.data!.length
              ))
            ]
          );
        }
      ),
    );
  }
}